package com.meidusa.amoeba.jdbc;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import com.meidusa.amoeba.util.JVM;

/**
 * in order to adapt jdbc4 and jdbc3 , copy some jdbc4 classes and pack them into jdbc4_part.jar
 * @author struct
 *
 */
public class ConnectionWrapper implements Connection{
	private Connection conn;
	public ConnectionWrapper(Connection conn){
		this.conn = conn;
	}
	public void clearWarnings() throws SQLException {
		conn.clearWarnings();
	}

	public void close() throws SQLException {
		conn.close();
	}

	public void commit() throws SQLException {
		conn.commit();
	}

	public Statement createStatement() throws SQLException {
		return conn.createStatement();
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		return conn.createStatement(resultSetType, resultSetConcurrency);
	}

	public Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public boolean getAutoCommit() throws SQLException {
		return conn.getAutoCommit();
	}

	public String getCatalog() throws SQLException {
		return conn.getCatalog();
	}

	public int getHoldability() throws SQLException {
		return conn.getHoldability();
	}

	public DatabaseMetaData getMetaData() throws SQLException {
		return conn.getMetaData();
	}

	public int getTransactionIsolation() throws SQLException {
		return conn.getTransactionIsolation();
	}

	public Map<String, Class<?>> getTypeMap() throws SQLException {
		return conn.getTypeMap();
	}

	public SQLWarning getWarnings() throws SQLException {
		return conn.getWarnings();
	}

	public boolean isClosed() throws SQLException {
		return conn.isClosed();
	}

	public boolean isReadOnly() throws SQLException {
		return conn.isReadOnly();
	}

	public String nativeSQL(String sql) throws SQLException {
		return conn.nativeSQL(sql);
	}

	public CallableStatement prepareCall(String sql) throws SQLException {
		return conn.prepareCall(sql);
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException {
		return conn.prepareStatement(sql);
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		return conn.prepareStatement(sql, autoGeneratedKeys);
	}

	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
			throws SQLException {
		return conn.prepareStatement(sql, columnIndexes);
	}

	public PreparedStatement prepareStatement(String sql, String[] columnNames)
			throws SQLException {
		return conn.prepareStatement(sql, columnNames);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		conn.releaseSavepoint(savepoint);
	}

	public void rollback() throws SQLException {
		conn.rollback();
	}

	public void rollback(Savepoint savepoint) throws SQLException {
		conn.rollback();
	}

	public void setAutoCommit(boolean autoCommit) throws SQLException {
		conn.setAutoCommit(autoCommit);
	}

	public void setCatalog(String catalog) throws SQLException {
		conn.setCatalog(catalog);
	}

	public void setHoldability(int holdability) throws SQLException {
		conn.setHoldability(holdability);
	}

	public void setReadOnly(boolean readOnly) throws SQLException {
		conn.setReadOnly(readOnly);
	}

	public Savepoint setSavepoint() throws SQLException {
		return conn.setSavepoint();
	}

	public Savepoint setSavepoint(String name) throws SQLException {
		return conn.setSavepoint(name);
	}

	public void setTransactionIsolation(int level) throws SQLException {
		conn.setTransactionIsolation(level);
	}

	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		conn.setTypeMap(map);
	}
	
	//following codes for jdk 1.6
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,Array.class,"createArrayOf",new Object[]{typeName,elements},new Class[]{String.class,Object[].class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public Blob createBlob() throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,Blob.class,"createBlob",(Object[])null,(Class[])null);
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public Clob createClob() throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,Clob.class,"createClob",(Object[])null,(Class[])null);
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public NClob createNClob() throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,NClob.class,"createNClob",(Object[])null,(Class[])null);
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public SQLXML createSQLXML() throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,SQLXML.class,"createSQLXML",(Object[])null,(Class[])null);
		}else{
			throw new UnsupportedOperationException();
		}
		
	}
	
	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,Struct.class,"createStruct",new Object[]{typeName,attributes},new Class[]{String.class,Object[].class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public Properties getClientInfo() throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,Properties.class,"getClientInfo",(Object[])null,(Class[])null);
		}else{
			throw new UnsupportedOperationException();
		}
	}
	public String getClientInfo(String name) throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,String.class,"getClientInfo",new Object[]{name},new Class[]{String.class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	public boolean isValid(int timeout) throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,boolean.class,"isValid",new Object[]{timeout},new Class[]{int.class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		if(JVM.is16()){
		  ClassWrapperUtil.invoke(conn,void.class,"setClientInfo",new Object[]{properties},new Class[]{Properties.class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		if(JVM.is16()){
		  ClassWrapperUtil.invoke(conn,void.class,"setClientInfo",new Object[]{name,value},new Class[]{String.class,String.class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,boolean.class,"isWrapperFor",new Object[]{iface},new Class[]{Class.class});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if(JVM.is16()){
			return ClassWrapperUtil.invoke(conn,iface,"unwrap",new Object[]{iface},new Class[]{iface});
		}else{
			throw new UnsupportedOperationException();
		}
	}
	
	//following codes for jdk 1.7
	public void abort(Executor executor) throws SQLException {
	  if (JVM.is17()) {
        ClassWrapperUtil.invoke(conn, void.class, "abort", new Object[]{executor}, new Class[]{Executor.class});
      }
	  else {
        throw new UnsupportedOperationException();
      }
	}
	public int getNetworkTimeout() throws SQLException {
	  if (JVM.is17()) {
        return ClassWrapperUtil.invoke(conn, int.class, "getNetworkTimeout", (Object[])null, (Class[])null);
      }
	  else {
        throw new UnsupportedOperationException();
      }
	}
	public String getSchema() throws SQLException {
	  if (JVM.is17()) {
        return ClassWrapperUtil.invoke(conn, String.class, "getSchema", (Object[])null, (Class[])null);
      }
	  else {
        throw new UnsupportedOperationException();
      }
	}
	public void setNetworkTimeout(Executor executor, int milliseconds)
			throws SQLException {
	  if (JVM.is17()) {
        ClassWrapperUtil.invoke(conn, void.class, "setNetworkTimeout", new Object[]{executor, milliseconds}, new Class[]{Executor.class, int.class});
      }
	  else {
        throw new UnsupportedOperationException();
      }
	}
	public void setSchema(String schema) throws SQLException {
	  if (JVM.is17()) {
        ClassWrapperUtil.invoke(conn, void.class, "setSchema", new Object[]{schema}, new Class[]{String.class});
      }
	  else {
        throw new UnsupportedOperationException();
      }
	}
}
